//
//  ONObjectDayAhead.m
//  Volatix
//
//  Created by Familly on 6/20/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONObjectDayAhead.h"
@interface ONObjectDayAhead ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end
@implementation ONObjectDayAhead
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    BOOL isParsing=NO;
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        /// bool
        isParsing=YES;
        // float
        
        
        self.congestion=[[self objectOrNilForKey:@"congestion" fromDictionary:dict] floatValue];
        self.loss=[[self objectOrNilForKey:@"loss" fromDictionary:dict] floatValue];
        self.price=[[self objectOrNilForKey:@"price" fromDictionary:dict] floatValue];
        self.hourending=[[self objectOrNilForKey:@"hourending" fromDictionary:dict] floatValue];
        NSString *strDate=[Utils objectForKeyNotNull:@"deliverydate" andDictionnary:dict];
        self.deliverydate = [Utils getDateTimeAtZeroTime:strDate];
        
        /// iso
        self.ISO=[self objectOrNilForKey:@"iso" fromDictionary:dict];
        self.LOADZONE=[self objectOrNilForKey:@"loadZone" fromDictionary:dict];
        self.POWERPOINT=[self objectOrNilForKey:@"powerpoint" fromDictionary:dict];
        
        
      
        
        // Column Dynamic
        
        NSMutableArray * arrayColumn= [APP_DELEGATE getingListDynamicColumnNameRealTime:false];
        if(_dynamicColumnData!=nil)
        {
            [_dynamicColumnData removeAllObjects];
        }
        else
        {
            _dynamicColumnData = [[NSMutableArray alloc] init];
        }
        
        for (int index=0; index<[arrayColumn count]; index++) {
            ///
            ONColumnTitleObject *keyColumn = [arrayColumn objectAtIndex:index];
            ONDataDynamicColumn * item = [[ONDataDynamicColumn alloc] init];
            item.nameColumn = keyColumn.name;
            if(item.nameColumn!=nil)
            {
                // NSLog(@"%@" ,[NSString stringWithFormat:@"is%@Increased",item.nameColumn]);
                item.Value=[[self objectOrNilForKey:item.nameColumn fromDictionary:dict] floatValue];
            }
            if(item.Value!=0)
            {
                [_dynamicColumnData addObject:item];
            }
            
        }
        
        
    }
    
    return (isParsing)?self:nil;
    
}
#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}
@end
